﻿using DevExpress.Data;
using DevExpress.Web.ASPxGridView;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility1;

namespace eDream.Page
{
    public partial class hhDoanhsonhom : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int month = DateTime.Now.Month;
                int year = DateTime.Now.Year;
                DateTime lastday = GetLastDayOfMonth(year, month);
                DateTime dtfrom = GetfirstDayOfMonth(year, month);
                dteFrom.Date = dtfrom;
                dteTo.Date = lastday;


            }
            dteFrom.Enabled = true;
            dteFrom.ReadOnly = false;
        }
        public static DateTime GetLastDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, DateTime.DaysInMonth(year, month));
        }

        public static DateTime GetfirstDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, 1);
        }
        protected void ASPxCallbackPanel1_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            Bindata(dteFrom.Date.ToString("MM/dd/yyyy"), dteTo.Date.ToString("MM/dd/yyyy"), 100);
        }

        protected void grvResult_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            try
            {
                double hasError = double.Parse((e.GetValue("Dadong")).ToString());
                if (hasError < 590 && hasError > 99)
                {
                    e.Row.ForeColor = System.Drawing.Color.FromName("#007ACC");
                }
                else if (hasError < 100)
                    e.Row.ForeColor = System.Drawing.Color.Red;
            }
            catch (Exception) { }
        }

        void Bindata(string dateFrom, string dataTo, int tang)
        {
            Xuly xl = new Xuly();
            try
            {
                UserDB user_obj = (UserDB)Session[Global.SESS_USER];
                DataTable tb = new DataTable();
                string sql = string.Format("select [index],UserName,Fullname,DateBegin,capbac,nguoigioithieu,Theloai,Dadong from uf_thongkedoanhsothe_bydate({0},'{1}','{2}',{3})", user_obj.ID, dateFrom, dataTo, tang);
                tb = xl.ExcuteSQL_Table(sql);

                //tb.Rows.Add(2,"A0007","Phan Huu Tinh","05/05/2013","Thanh Vien",null,"Gioi Thieu",590);
                //tb.Rows.Add(3, "A0008", "Phan Huu Tinh 2", "05/05/2013", "Thanh Vien","Gioi Thieu", null, 500);
                grvResult.DataSource = tb;
                grvResult.DataBind();
            }
            catch (Exception) { }

        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            Bindata(dteFrom.Date.ToString("MM/dd/yyyy"), dteTo.Date.ToString("MM/dd/yyyy"), 100);
        }

        protected void grvResult_Load(object sender, EventArgs e)
        {
            Bindata(dteFrom.Date.ToString("MM/dd/yyyy"), dteTo.Date.ToString("MM/dd/yyyy"), 100);
        }

        double totalSum=0;
        protected void grvResult_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
        {
            ASPxSummaryItem item = e.Item as ASPxSummaryItem;
            if (item.FieldName == "Dadong")
            {
                if (e.SummaryProcess == CustomSummaryProcess.Start)
                {
                    totalSum = 0;
                }
                else if (e.SummaryProcess == CustomSummaryProcess.Calculate)
                {
                    if (Convert.ToDouble(e.FieldValue) == 590)
                        totalSum += 500;
                    e.TotalValue = totalSum;//Convert.ToInt32((grvResult.GetRow(3) as DataRowView)["Dadong"]);
                    e.TotalValueReady = true;

                }
            }
            
        }
    }
}